<?php
namespace app\admin\controller;
use think\Db;
use think\Exception;

	class Rule extends Base
	{
		/**
		 * 权限管理列表
		 */
		public function Rule(){
			if($this->request->isPost() && isset($_POST['lock'])){
				if(input('post.lock')==1){
					db('system_module')->where('mod_id='.input('post.id'))->update(['visible'=>0]);
				}else{
					db('system_module')->where('mod_id='.input('post.id'))->update(['visible'=>1]);
				};
				return $this->success('1');
			}
			$condition=[];
			if(input('ctl')){
				$condition[]=['ctl','like',"%".input('ctl')."%"];
			}
			if(input('act')){
				$condition[]=['act','like',"%".input('act')."%"];
			}
			if(input('title')){
				$condition[]=['title','like',"%".input('title')."%"];
			}
			$info = model("SystemModule")
				->order(['ctl','parent_id'])
				->where($condition)
				->paginate([
					'list_rows'=>10,
					'query'=>input('param.'),
				]);
			$total = model('SystemModule')->count();
			$this->assign('total',$total);
			$this->assign('info',$info);
			return $this->fetch();
		}

		/**
		 * 权限添加
		 */
		public function add(){
			if($this->request->isPost()){
				//dump(input());
				$validate = Validate('Module');
				if(input('post.ctl')!=null){
					$ctl = input('post.ctl');
					$act = '';
					$res = $validate->scene('ctl')->check(['ctl'=>$ctl]);
					if(!class_exists("\app\admin\controller\\".$ctl)){
						return $this->error("该类不存在");
					}
				}else{
					$arr = model("SystemModule")->where('mod_id',input('post.parent'))->select();
					$ctl = $arr[0]['ctl'];
					$act = input('post.act');
					$res = $validate->scene('act')->check(['act'=>$act]);
					if(!method_exists("\app\admin\controller\\".$ctl,$act)){
						return $this->error("该类中不存在该方法");
					}
				}
				if(!$res){
					return $this->error($validate->getError());
				}
				$re = model("SystemModule")->checkAction($ctl,$act);
				if($re != null){
					return $this->error('该顶级类已存在或该类中已经存在该方法');
				}
				if(input('post.ctl')!=null){
					$r = model("SystemModule")->insert([
						'module'   => 'menu',
    					'level'    => 1,
    					'ctl'      => ucfirst($ctl),
    					'title'    => input('post.name'),
    					'visible'  => input('post.visible'),
    					'parent_id'=> 0,
					]);
				}else{
					if($arr[0]['level']==1){
						$module = 'menu';
						$level = 2;
					}else{
						$module = 'module';
						$level = 3;
					}
					$r = model("SystemModule")->insert([
						'module'   => $module,
    					'level'    => $level,
    					'act'      => $act,
    					'ctl'      => ucfirst($ctl),
    					'title'    => input('post.name'),
    					'visible'  => input('post.visible'),
    					'parent_id'=> input('post.parent')
					]);
				}
				if($r > 0){
					return $this->success('添加成功');
				}else{
					return $this->error('添加失败');
				}
			}else{
				$info = model("SystemModule")->select();
				$this->assign('info',$info);
				return $this->fetch();
			}
		}
		/**
		 * 权限删除
		 */
		public function del(){
			$id=input('post.id');
			if(!empty(input('post.allid'))){
	    		$id=substr(input('post.allid'),0,-1);
	    	}
	    	if(input('force')){
	    		$re = model("SystemModule")->where("parent_id",'in',$id)->select();
	    		foreach ($re as $v) {
	    			model("SystemModule")->where("parent_id",'in',$v['mod_id'])->delete();
	    		}
	    		model("SystemModule")->where("parent_id",'in',$id)->delete();
	    	}else{
	    		$re = model("SystemModule")->where("parent_id",'in',$id)->find();
		    	if($re != null){
		    		return ['code'=>3];
		    	}
	    	}
	    	Db::startTrans();
	    	try{
		    	$res=model("SystemModule")->where("mod_id",'in',$id)->delete();
		    	if($res==0){
		    		throw new Exception("删除失败",2);
		    	}
		    	$res=model("Access")->where("right_id",'in',$id)->delete();
		    	Db::commit();
	    		throw new Exception("删除成功",1);
		    }catch (\Exception $e){
				Db::rollback();
				if($e->getCode()===1){
					return $this->success($e->getMessage());
				}else{
					return $this->error($e->getMessage());
				}
	    	}
		}

		/**
		 * 权限修改
		 */
		public function edit(){
			if($this->request->isPost()){
				$validate = Validate('Module');
				if(input('post.ctl')!=null){
					$ctl = input('post.ctl');
					$act = '';
					$res = $validate->scene('ctl')->check(['ctl'=>$ctl]);
				}else{
					$arr = model("SystemModule")->where('mod_id',input('post.parent'))->select();
					$ctl = $arr[0]['ctl'];
					$act = input('post.act');
					$res = $validate->scene('act')->check(['act'=>$act]);
				}
				if(!$res){
					return $this->error($validate->getError());
				}
				$re = model("SystemModule")->checkAction($ctl,$act,input('mod_id'));
				if($re != null){
					return $this->error('该顶级类已存在或该类中已经存在该方法');
				}
				if(input('post.ctl')!=null){
					$r = model("SystemModule")->where('mod_id',input("post.mod_id"))->update([
						'module'   => 'menu',
    					'level'    => 1,
    					'ctl'      => ucfirst($ctl),
    					'title'    => input('post.name'),
    					'visible'  => input('post.visible'),
    					'parent_id'=> 0,
					]);
				}else{
					if($arr[0]['level']==1){
						$module = 'menu';
						$level = 2;
					}else{
						$module = 'module';
						$level = 3;
					}
					$r = model("SystemModule")->where('mod_id',input("post.mod_id"))->update([
						'module'   => $module,
    					'level'    => $level,
    					'act'      => $act,
    					'ctl'      => ucfirst($ctl),
    					'title'    => input('post.name'),
    					'visible'  => input('post.visible'),
    					'parent_id'=> input('post.parent')
					]);
				}
				if($r > 0){
					return $this->success('修改成功');
				}else{
					return $this->error('未作任何修改');
				}
			}else{
				$data = model("SystemModule")->where("mod_id",input('mod_id'))->select();
				$a = model("SystemModule")->where("parent_id",input('mod_id'))->find();
				$info = model("SystemModule")->select();
				if($a != null){
					$this->assign('cou',0);
				}else{
					$this->assign('cou',1);
				}
				$this->assign('mod_id',input('mod_id'));
				$this->assign('data',$data[0]);
				$this->assign('info',$info);
				return $this->fetch();
			}
		}
	}